package StrategyCode.ConcreteStrategy;
import StrategyCode.EncryptStrategy;

public class CaesarEncrypt extends EncryptStrategy {
    //位移量
    private int shift;

    public CaesarEncrypt(int shift) {
        this.shift = shift;
    }

    @Override
    public String encrypt(String data) {
        StringBuilder encrypted = new StringBuilder();
        for (char character : data.toCharArray()) {
            if (Character.isLetter(character)) {
                char shiftedChar = (char) (character + shift);
                if (Character.isLowerCase(character) && shiftedChar > 'z' ||
                        Character.isUpperCase(character) && shiftedChar > 'Z') {
                    shiftedChar -= 26;
                }
                encrypted.append(shiftedChar);
            } else {
                encrypted.append(character);
            }
        }
        return encrypted.toString();
    }
}

